#include "StdAfx.h"

ToolBar::ToolBar(ui32 y, ui32 height) : m_text("Fonts\\FrizQT__.ttf", "Friz Quadrata TT", 7, 14)
{
	m_position.x = 0;
	m_position.y = (float)y;
	m_size.x = (float)sD3D.GetSize().x;
	m_size.y = height;

	float x = m_position.x;
	float cx = m_size.x;
	float cy = m_size.y;

	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = (float)x;
	m_baseVertices[0].y = (float)y;
	m_baseVertices[0].u = 60.0f / 256.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = (float)x + cx;
	m_baseVertices[1].y = (float)y;
	m_baseVertices[1].u = 60.0f / 256.0f;
	m_baseVertices[1].v = 1.0f;

	m_baseVertices[2].x = (float)x + cx;
	m_baseVertices[2].y = (float)y + cy;
	m_baseVertices[2].u = 4.0f / 256.0f;
	m_baseVertices[2].v = 1.0f;

	m_baseVertices[3].x = (float)x;
	m_baseVertices[3].y = (float)y + cy;
	m_baseVertices[3].u = 4.0f / 256.0f;
	m_baseVertices[3].v = 0.0f;

	m_baseTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\Tooltips\\ChatBubble-Backdrop.blp");
}

void ToolBar::Resize(ui32 y)
{
	float diffy = (float)y - m_position.y;
	m_position.y = (float)y;
	m_size.x = (float)sD3D.GetSize().x;
	if(m_size.y + y > sD3D.GetSize().y)
		m_size.y = sD3D.GetSize().y - y;
	float x = m_position.x;
	float cx = m_size.x;
	float cy = m_size.y;

	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = (float)x;
	m_baseVertices[0].y = (float)y;
	m_baseVertices[0].u = 60.0f / 256.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = (float)x + cx;
	m_baseVertices[1].y = (float)y;
	m_baseVertices[1].u = 60.0f / 256.0f;
	m_baseVertices[1].v = 1.0f;

	m_baseVertices[2].x = (float)x + cx;
	m_baseVertices[2].y = (float)y + cy;
	m_baseVertices[2].u = 4.0f / 256.0f;
	m_baseVertices[2].v = 1.0f;

	m_baseVertices[3].x = (float)x;
	m_baseVertices[3].y = (float)y + cy;
	m_baseVertices[3].u = 4.0f / 256.0f;
	m_baseVertices[3].v = 0.0f;

	std::set<UIButton<ToolBar>*>::iterator itr = m_buttons.begin();
	std::set<UIButton<ToolBar>*>::iterator end = m_buttons.end();

	for( ; itr != end; ++itr)
		(*itr)->ModPosition(0, diffy);

	std::for_each(m_icons.begin(), m_icons.end(),
	[&diffy, this](IconButton* ib)
	{ 
		ib->ModPosition(0, (i32)diffy);
		ib->SetSize(m_size.y - 12);
	}
	);
}

void ToolBar::Draw()
{
	if(!m_visible || sD3D.GetAppState() != STATE_WORLD)
		return;

	sD3D->SetFVF(VERTEX3FRHWT);
	sD3D->SetTexture(0, *m_baseTexture);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_baseVertices, sizeof(Vertex3FRHWT));

	std::set<UIButton<ToolBar>*>::iterator itr = m_buttons.begin();
	std::set<UIButton<ToolBar>*>::iterator end = m_buttons.end();

	for( ; itr != end; ++itr)
		(*itr)->Draw();

	std::vector<ToolbarText>::iterator tItr = m_texts.begin();
	std::vector<ToolbarText>::iterator tEnd = m_texts.end();

	for( ; tItr != tEnd; ++tItr)
		m_text.DrawTextW(ui32(m_position.x + (*tItr).posX), ui32(m_position.y + 8), (*tItr).length, 30, (*tItr).szText);

	std::for_each(m_icons.begin(), m_icons.end(),
		[](IconButton* ib) { ib->Draw(); } );
}

void ToolBar::AddButton(ui32 x, ui32 w, std::string szText, Panel* pPanel)
{
	UIButton<ToolBar>* bt = new UIButton<ToolBar>(this, Vertex2F((float)x, m_position.y + 6), Vertex2F((float)w, 22), szText);
	bt->pData = (void*)pPanel;
	bt->SetOnClick(&ToolBar::ButtonClicked);
	m_buttons.insert(bt);
}

void ToolBar::ButtonClicked(UIButton<ToolBar>* pButton)
{
	if(pButton && pButton->pData)
	{
		if(((Panel*)pButton->pData)->m_visible)
			((Panel*)pButton->pData)->FadeOut();
		else
			((Panel*)pButton->pData)->FadeIn();
	}
	else if(pButton && !pButton->pData)
		pButton->OnMouseClick();
}

void ToolBar::MouseActivity(bool left, Vertex2F pos, bool up)
{
	std::set<UIButton<ToolBar>*>::iterator itr = m_buttons.begin();
	std::set<UIButton<ToolBar>*>::iterator end = m_buttons.end();

	for( ; itr != end; ++itr)
	{
		Vertex2F& s = (*itr)->m_size;
		Vertex2F& p = (*itr)->m_position;
		if(pos.x >= p.x && pos.y >= p.y && pos.x <= p.x + s.x && pos.y <= p.y + s.y && (*itr)->m_visible)
		{
			if(up)
			{
				(*itr)->m_state = 0;
				(*itr)->OnMouseClick();
				(*itr)->MouseActivity(left, pos, up);
			}
			else
			{
				(*itr)->m_state = 1;
				(*itr)->MouseActivity(left, pos, up);
			}
		}
		else if((*itr)->m_state)
		{
			(*itr)->m_state = 0;
			(*itr)->OnMouseClick();
			(*itr)->MouseActivity(left, pos, true);
		}
	}

	std::for_each(m_icons.begin(), m_icons.end(),
		[left, pos, up](IconButton* ib) { ib->MouseActivity(left, pos, up); });
}

void ToolBar::OnMouseMove(ui32 x, ui32 y, bool leftDown, bool rightDown)
{
	std::for_each(m_icons.begin(), m_icons.end(),
		[x, y, leftDown, rightDown](IconButton* ib) { ((UIElement*)ib)->OnMouseMove(x, y, leftDown, rightDown); } );
}